package cn.szbo.jksystem.core;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页辅助类
 *
 * @author johnny
 * @version 2012-3-13 下午1:46:00
 *
 * @param <T>
 */
public class Pagination<T> {

    /**
     * 默认页号
     */
    public static final int DEFAULT_PAGE_INDEX = 1;

    /**
     * 默认每页显示数据
     */
    public static final int DEFAULT_PAGESIZE = 10;

    /**
     * 每页显示记录数
     */
    private int pageSize = DEFAULT_PAGESIZE;

    /**
     * 总页数
     */
    private int totalPages;

    /**
     * 总记录数
     */
    private int totalRecords;

    /**
     * 当前页
     */
    private int currentPage;

    /**
     * 当前记录起始索引
     */
    private int startRow;

    /**
     * 分页查询条件
     */
    private Map<String, Object> params = new HashMap<String, Object>();

    /**
     * 结果集存放List
     */
    private List<T> results;

    /**
     * 计算总页数
     *
     * @return
     */
    public int countTotalPages() {
        if (totalRecords % pageSize == 0)
            totalPages = totalRecords / pageSize;
        else
            totalPages = totalRecords / pageSize + 1;
        return totalPages;
    }

    /**
     * 计算当前页
     *
     * @return
     */
    public int countCurrentPage() {
        if (currentPage <= 0)
            currentPage = 1;
        if (currentPage > totalPages)
            currentPage = totalPages;
        return currentPage;
    }

    /**
     * 计算当前记录起始索引
     *
     * @return
     */
    public int countStartRow() {
        startRow = (countCurrentPage() - 1) * pageSize;
        if (startRow < 0)
            startRow = 0;
        return startRow;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public List<T> getResults() {
        return results;
    }

    public void setResults(List<T> results) {
        this.results = results;
    }

}